package dao;

import beans.User;
import beans.UserGroupRelation;
import db.DBQuery;
import db.Entity;

import java.util.List;

public class UserGroupRelationDAO extends Entity {

    public final static UserGroupRelationDAO ME = new UserGroupRelationDAO();

    @Override
    public String tableName() {
        return "wb_user_group_relation";
    }

    public long addRelation(long user_id, long group_id) {
        UserGroupRelation userGroupRelation = new UserGroupRelation();
        userGroupRelation.setUser_id(user_id);
        userGroupRelation.setGroup_id(group_id);
        return userGroupRelation.SaveToDB();
    }

    public UserGroupRelation getRelation(long user_id, long group_id) {
        String sql = "select * from " + tableName() + " where user_id = ? and group_id = ? limit 1";
        return DBQuery.read_bean(UserGroupRelation.class, sql, user_id, group_id);
    }

    public List<User> getGroupMemberList(long group_id) {
        String sql = "select * from " + UserDAO.ME.tableName() + " where id in " +
                "(select user_id from " + tableName() + " where group_id = ?)";
        return DBQuery.query_list(User.class, sql, group_id);
    }

    public long deleteGroupRelation(long group_id) {
        String sql = "delete from " + tableName() + " where group_id = ?";
        return DBQuery.update(sql, group_id);
    }
}
